<?php
openlog('Sistema control de asistencias', LOG_NDELAY, LOG_USER);
// Include the main TCPDF library (search for installation path).
require_once('../js/libs/tcpdf/tcpdf.php');
require '../mysql/mysql_connect.php';

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set default header data
$pdf->SetHeaderData('', '', PDF_HEADER_TITLE . ' Obrero del mes de ' . $_GET['mes'], '');
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$opera = new OperacionesMysql();
$result1 = $opera->query("SELECT cedula, nombre, apellido FROM control_de_asistencia.personal where cargo_id_cargo=3;");
$filasPersonal = "";
$indice = 1;
if (is_array($result1)) {
    foreach ($result1 as $k => $v) {
        $contDias = 0;
        $td = "";
        $dias = '<td><table border="1"><tr align="center">';
        $result = $opera->query("SELECT DATE_FORMAT(a.fecha,'%d') as fecha FROM asistencia as a, personal as p where a.personal_cedula=p.cedula and p.cedula='" . $v['cedula'] . "' and tipo='salida' and  DATE_FORMAT(a.fecha,'%m %Y')='".$_GET['fecha']."' order by a.fecha;");
        if (is_array($result)) {

            foreach ($result as $item) {
                $td.="<td>" . $item['fecha'] . "</td>";
                $contDias++;
            }
        } else {
            $td.="<td></td>";
        }
        $dias.=$td;
        $dias.='</tr></table></td><td >' . $contDias++ . '</td>';
        $filasPersonal .= '<tr align="left">' . '<td>' . $indice . '</td>' . '<td>' . $v['nombre'] . '</td>' . '<td>' . $v['apellido'] . '</td>' . $dias . '</tr>';
        $indice++;
    }
}
// add a page
$pdf->AddPage();
$html = '<table border="1">
    <tr align="center">
        <td rowspan="1" width="3%" bgcolor="#f5f5f5">N°</td>
        <td rowspan="1" bgcolor="#f5f5f5">Nombre</td>
        <td rowspan="1s" bgcolor="#f5f5f5">Apellido</td>
        <td colspan="1" width="52%" bgcolor="#f5f5f5">Asistencias diarias</td>
        <td colspan="1" bgcolor="#f5f5f5" width="5%">Total Dias</td>
    </tr>'
        . $filasPersonal . '
   
</table>';
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
//----Salida del reporte
$pdf->Output("../reporte/obrero.pdf", 'F');
closelog();
?>
